package com.lsmgc.cms.servlet.article;

import com.alibaba.fastjson.JSON;
import com.lsmgc.cms.entity.article.Article;
import com.lsmgc.cms.entity.article.ArticleQuery;
import com.lsmgc.cms.service.article.ArticleService;
import com.lsmgc.cms.util.RequestUtils;
import com.lsmgc.cms.util.ServletUtils;
import com.lsmgc.cms.util.json.JsonReulst;
import com.lsmgc.cms.util.page.Paginable;
import com.lsmgc.cms.util.page.Pagination;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AdminArticleServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        switch (path) {
            case "/admin/article/list":
                try {
                    articleList(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/article/add":
                articleAdd(request, response);
                break;
            case "/admin/article/upd":
                try {
                    articleUpd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/article/list":
                ajaxArticleList(request, response);
                break;
            case "/admin/ajax/article/add":
                try {
                    ajaxArticleAdd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/article/upd":
                try {
                    ajaxArticleUpd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/article/del":
                try {
                    ajaxArticleDel(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    /**
     * 文章列表
     *
     * @param request
     * @param response
     */
    private void articleList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        ArticleQuery articleQuery = new ArticleQuery();
        articleQuery.setTitle(request.getParameter("title"));
        articleQuery.setPageNo(Pagination.cpn(RequestUtils.getInt(request, "pageNo")));
        articleQuery.setPageSize(5);
        Pagination pagination = articleService.getArticleListPage(articleQuery);
        request.setAttribute("pagination", pagination);
        request.setAttribute("articleQuery", articleQuery);
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/article/article-list")).forward(request, response);
    }

    /**
     * 文章添加
     *
     * @param request
     * @param response
     */
    private void articleAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/article/article-add")).forward(request, response);
    }

    /**
     * 文章修改
     *
     * @param request
     * @param response
     */
    private void articleUpd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Long id = RequestUtils.getLong(request, "id");
        Article article = articleService.getArticleById(id);
        request.setAttribute("article", article);
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/article/article-upd")).forward(request, response);
    }

    /**
     * 文章列表
     *
     * @param request
     * @param response
     */
    private void ajaxArticleList(HttpServletRequest request, HttpServletResponse response) {

    }

    /**
     * 文章添加
     *
     * @param request
     * @param response
     */
    private void ajaxArticleAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        Article article = new Article();
        article.setTitle(request.getParameter("title"));
        article.setContent(request.getParameter("content"));
        article.setImg(request.getParameter("img"));
        article.setAddTime(new Date());
        boolean b = articleService.addArticle(article);
        PrintWriter writer = response.getWriter();
        if (b) {
            writer.write(JSON.toJSONString(JsonReulst.yes()));
        } else {
            writer.write(JSON.toJSONString(JsonReulst.no()));
        }
    }

    /**
     * 文章修改
     *
     * @param request
     * @param response
     */
    private void ajaxArticleUpd(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Article article = new Article();
        article.setId(RequestUtils.getLong(request, "id"));
        article.setTitle(request.getParameter("title"));
        article.setContent(request.getParameter("content"));
        article.setImg(request.getParameter("img"));
        boolean b = articleService.updateArticleById(article);
        PrintWriter writer = response.getWriter();
        if (b) {
            writer.write(JSON.toJSONString(JsonReulst.yes()));
        } else {
            writer.write(JSON.toJSONString(JsonReulst.no()));
        }
    }

    /**
     * 文章删除
     *
     * @param request
     * @param response
     */
    private void ajaxArticleDel(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Long id = RequestUtils.getLong(request, "id");
        boolean b = articleService.deleteArticleById(id);
        PrintWriter writer = response.getWriter();
        if (b) {
            writer.write(JSON.toJSONString(JsonReulst.yes()));
        } else {
            writer.write(JSON.toJSONString(JsonReulst.no()));
        }
    }
}
